﻿@model NhConcurrencyOnAspNetMvc.Models.Song

@{
    ViewBag.Title = "Input";
}

<h2>Input</h2>


@DateTime.Now

<script src="@Url.Content("~/Scripts/jquery.validate.min.js")" type="text/javascript"></script>
<script src="@Url.Content("~/Scripts/jquery.validate.unobtrusive.min.js")" type="text/javascript"></script>
<script src="/Scripts/IEnableMuch/ienablemuch.js" type="text/javascript"></script>

@{ string controllerName = (string) ViewContext.RouteData.Values["Controller"]; }

@using (Html.BeginForm("Save", controllerName) )
{

    @Html.HiddenFor(x => x.SongId)
    @Html.HiddenFor(x => x.Version)
    <fieldset>
        <legend>Song</legend>

        <div class="editor-label">
            @Html.LabelFor(model => model.SongName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.SongName)
            @Html.ValidationMessageFor(model => model.SongName)
        </div>

        <div class="editor-label">
            @Html.LabelFor(model => model.AlbumName)
        </div>
        <div class="editor-field">
            @Html.EditorFor(model => model.AlbumName)
            @Html.ValidationMessageFor(model => model.AlbumName)
        </div>

        <p>
            <input type="submit" value="Save" />
        </p>
    </fieldset>
    
    
    
    @Html.JsAccessibleValidationSummary(true)
    
         
}


<div>
    @Html.ActionLink("Back to List", "Index")
</div>



@ViewBag.Message



<script type="text/javascript">

    $(function () {

        $('input[type=submit]').click(function (e) {            
            e.preventDefault();

            if ($('form').valid()) {

                //alert('test');
                $.ajax({
                    url: '/Song/SaveViaAjax',
                    type: 'POST',
                    data: $('form').serialize(),
                    dataType: 'json',
                    success: function (data) {


                        var isOk = $('form').modelValidation(data);


                        if (isOk) {
                            $('input[name=SongId]').val(data.SongId);
                            $('input[name=Version]').val(data.Version);

                            alert('Saved.');
                        }

                    },
                    error: function (xhr, err) {
                        alert("readyState: " + xhr.readyState + "\nstatus: " + xhr.status);
                        alert("responseText: " + xhr.responseText);
                    }
                }); //ajax

            } //end if valid

        }); // submit


    });   // ready
</script>

